package com.zzu.wardrobe.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zzu.wardrobe.entity.User;
import com.zzu.wardrobe.service.UserServiceImpl;
import com.zzu.wardrobe.util.JsonUtil;

import net.sf.json.JSONObject;

public class UserAction extends ActionSupport implements ModelDriven<User> {
	private User user = new User();
	// private Object values;
	private List<User> users;
	private UserServiceImpl userService;

	Map<String, Object> res = new HashMap<String, Object>();

	public Map<String, Object> getRes() {
		return res;
	}

	public void setRes(Map<String, Object> res) {
		this.res = res;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public UserServiceImpl getUserService() {
		return userService;
	}

	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}

	public String login() {

		System.out.println("**" + user.getUsername());
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println("request2:" + request.getSession().getAttribute("username"));
		System.out.println("context2:" + ActionContext.getContext().getSession().get("username"));
			
		// System.out.println(request.toString());
		// String path=request.getRequestURI();

		HttpServletResponse response = ServletActionContext.getResponse();
		/**
		 * 允许跨域
		 */
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Headers", "*");
		int flag = 0;
		try {
			String jsoon = JsonUtil.getStrResponse();
			JSONObject jsonObject = JSONObject.fromObject(jsoon);
			User userjson = (User) JSONObject.toBean(jsonObject, User.class);
			user.setUsername(userjson.getUsername());
			user.setPassword(userjson.getPassword());
			flag = userService.login(user);
			// JSONObject jsoon=JsonReader.receivePost(request);

			// JSONObject user = JSONObject.fromObject(jsoon);
			System.out.println(userjson.toString());
			Gson gson = new Gson();
			PrintWriter out = response.getWriter();
			System.out.println("flag:" + flag);
			// Gson 传输数据、
			System.out.println("&*" + request.getSession().getAttribute("username"));
			if (flag > 0 && request.getSession().getAttribute("username") != null) {
				res.put("code", 2);
				res.put("msg", "您已登录，请不要重复登录");
				flag = -1;
			}
			if (flag > 0) {
				ActionContext.getContext().getSession().put("username", user.getUsername());
				// request.getSession().setAttribute("username", user.getUsername());
				System.out.println("request:" + request.getSession().getAttribute("username"));
				System.out.println("context:" + ActionContext.getContext().getSession().get("username"));
				res.put("code", 0);
				res.put("msg", "欢迎回来");
			}
			if (flag == 0) {
				res.put("code", 1);
				res.put("msg", "登录失败，用户名或密码错误");
			}
			String msg = gson.toJson(res);
			response.getWriter().print(msg);
		} catch (Exception e) {
			HttpServletResponse resp = ServletActionContext.getResponse();
			Gson gson = new Gson();
			res.put("code", 6);
			res.put("msg", "测试接口");
			String msg = gson.toJson(res);
			try {
				resp.getWriter().print(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return null;
	}
}
